<?php

declare(strict_types=1);

namespace App\Job;

use App\Model\CloseFriend;
use App\Model\HostPushLog;
use App\Model\Message;
use App\Model\UserBlacklist;
use App\Model\UserProfile;
use Hyperf\AsyncQueue\Job;

/**
 * 主播自动打招呼任务
 */
class HostGreetJob extends Job
{
    public $params;

    protected $maxAttempts = 1;

    public function __construct($params)
    {
        $this->params = $params;
    }

    public function handle()
    {
        $userId = $this->params['user_id'] ?? 0;
        log_user_result($userId, $this->params);
        $pushNum = $this->params['push_num'] ?? 0;
        if (!$pushNum) return true;
        //推送
        $batchNo = $this->params['batch_no'] ?? 1;
        $greetType = $this->params['greet_type'] ?? HostPushLog::TYPE_SYS_PACK;
        $hostToUser = $this->params['host_to_user'] ?? 0;

        //把主播推给选定男用户
        if ($hostToUser) {
            $this->doPushHost($userId, $pushNum, $batchNo, $greetType);
        }
        //选取主播推给男用户
        else {
            $this->doPush($userId, $pushNum, $batchNo, $greetType);
        }
        return true;
    }

    /**
     *  推送三件套消息（推给主播选取的男用户）
     */
    function doPushHost($hostId = 0, $pushNum = 1, $batchNo = 1, $greetType = 1)
    {
        //选取在线的用户
        $onlineUsers =
            check_dev_env() ?
            UserProfile::getRecUsers(UserProfile::ROLE_USER, 1) :
            UserProfile::getNewUsers(UserProfile::ROLE_USER, 300, 1);
        //排除已经推送过的主播
        $offTime = env('HOST_PUSH_PACKAGE_OFFTIME', 3600 * 12);
        $pushedUsers = HostPushLog::getPushUserIds($hostId, $offTime);
        //排斥有亲密度的用户
        $closeUsers = CloseFriend::getFrdList($hostId);
        //有效主播
        $validUsers = array_diff($onlineUsers, $pushedUsers, $closeUsers);
        $validUsers = array_slice($validUsers, 0, $pushNum);

        foreach ($validUsers as $userId) {
            //推送三件套
            $pushRet = Message::sendProfilePackage($userId, $hostId);
            //记录
            HostPushLog::addLog($hostId, $userId, $greetType, $batchNo);
        }

        //透传
        Message::sendSlient($hostId, 'HIT_LOOP', ['hit_num' => $pushNum, 'hit_speed' => 2]);
        log_user_result($hostId, "Batch:{$batchNo}==UserIds:" . json_encode($validUsers));
    }

    /**
     *  推送三件套消息（选取主播推给男用户）
     */
    function doPush($userId = 0, $pushNum = 1, $batchNo = 1, $greetType = 1)
    {
        //选取在线的主播
        $onlineHosts =
            check_dev_env() ?
            UserProfile::getRecHosts(0) :
            UserProfile::getOnlineUsers(UserProfile::ROLE_HOST, 300);
        //排除已经推送过的主播
        $offTime = env('HOST_PUSH_PACKAGE_OFFTIME', 3600 * 12);
        $pushedHosts = HostPushLog::getPushHostIds($userId, $offTime);
        //排斥有亲密度的主播
        $closeHosts = CloseFriend::getFrdList($userId);
        //排除拉黑的
        $blackHosts = UserBlacklist::getList($userId, 'uid');
        //有效主播
        $validHosts = array_diff($onlineHosts, $pushedHosts, $closeHosts, $blackHosts);
        shuffle($validHosts);
        // shuffle($validHosts);
        $validHosts = array_slice($validHosts, 0, $pushNum);
        foreach ($validHosts as $hostId) {
            //推送三件套
            $pushRet = Message::sendProfilePackage($userId, $hostId);
            //记录
            HostPushLog::addLog($hostId, $userId, $greetType, $batchNo);
        }
        log_user_result($userId, "Batch:{$batchNo}==HostIds:" . json_encode($validHosts));
    }
}
